home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 09 Laramée / Entities.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-21  |  5.0 KB  |  170 lines

  1. /****************************************************************
  2.  * ENTITIES.H
  3.  * Definitions of the classes of objects found in the world,
  4.  * except for the troll himself
  5.  ***************************************************************/
  6.  
  7. #include "Globals.h"
  8. #include "WorldGrid.h"
  9.  
  10. #ifndef ENTITIES_H
  11. #define ENTITIES_H
  12.  
  13. class Troll;
  14.  
  15. /***************************************************************
  16.  * CLASS Entity
  17.  * A virtual base class for the hierarchy of entities.  Its
  18.  * structure is based on my article in Game Programming Gems 2,
  19.  * which you should have read by now ;-)
  20.  **************************************************************/
  21.  
  22. class Entity
  23. {
  24.     // Storage common to all classes of entities: Positional data and
  25.     // an ID to register their actions with the WorldGrid
  26.     int PosX, PosY;
  27.     int WorldID;
  28.  
  29. protected:
  30.     // A static pointer to the troll, shared by all entities which
  31.     // may need to send it messages
  32.     static Troll * ptrTroll;
  33.  
  34.     // A static pointer to the World Grid
  35.     static WorldGrid * ptrGrid;
  36.  
  37. public:
  38.     // Construction and access
  39.     Entity( int x, int y, int id ) : PosX( x ), PosY( y ), WorldID( id ) {}
  40.     virtual Entity * Clone() = 0;
  41.  
  42.     int GetX() { return PosX; }
  43.     int GetY() { return PosY; }
  44.     int GetWorldID() { return WorldID; }
  45.     void SetX( int x ) { PosX = x; }
  46.     void SetY( int y ) { PosY = y; }
  47.     static void AttachTroll( Troll & theTroll ) { ptrTroll = &theTroll; }
  48.     static void AttachGrid( WorldGrid & theGrid ) { ptrGrid = &theGrid; }
  49.  
  50.     // Movement on the grid
  51.     void MoveEntity( int dx, int dy ); 
  52.  
  53.     // The class this object advertises itself as.  Can change
  54.     // over time, so we can't get by with typeid
  55.     virtual int ExportedClass() = 0;
  56.  
  57.     // Interface call for whatever the entity does in each
  58.     // time step
  59.     virtual bool Update() = 0;
  60. };
  61.  
  62.  
  63. /*************************************************************
  64.  * CLASS Sheep
  65.  * The bottom of the food chain in this demo, the Sheep are
  66.  * there to be eaten by the troll.  All they ever do is wander
  67.  * and try to run away if they see a troll coming too close.
  68.  *************************************************************/
  69.  
  70. class Sheep : public Entity
  71. {
  72.     // Sheep that get eaten die
  73.     bool IsDead;
  74.  
  75. public:
  76.     // Construction and access
  77.     Sheep( int x, int y, int id ) : Entity( x, y, id ), IsDead( false ) 
  78.         { ptrGrid->Register( id, ExportedClass(), x, y ); }
  79.     Entity * Clone() { return( new Sheep( GetX(), GetY(), GetWorldID() ) ); }
  80.  
  81.     int ExportedClass() { return ENTITY_SHEEP; }
  82.  
  83.     bool Update();
  84. };
  85.  
  86.  
  87. /*************************************************************
  88.  * CLASS Knight
  89.  * The bad guys in this simulation.  Knights wander aimlessly
  90.  * until they detect a troll, then track it mercilessly until
  91.  * a fight to the death can ensue.
  92.  ************************************************************/
  93.  
  94. class Knight : public Entity
  95. {
  96.     // Knights wander around aimlessly until they smell a troll,
  97.     // and then they pursue it to the death
  98.     bool IsBerserk;
  99.     bool IsDead;
  100.  
  101. public:
  102.     Knight( int x, int y, int id ) : Entity( x, y, id ), IsBerserk( false ), IsDead( false )
  103.             { ptrGrid->Register( id, ExportedClass(), x, y ); }
  104.     Entity * Clone() { return( new Knight( GetX(), GetY(), GetWorldID() ) ); }
  105.  
  106.     int ExportedClass() { return ENTITY_KNIGHT; }
  107.  
  108.     bool Update();
  109. };
  110.  
  111.  
  112. /************************************************************
  113.  * CLASS Tower
  114.  * This is an immobile, indestructible guard tower manned by
  115.  * archers whofire at the troll from a distance
  116.  ***********************************************************/
  117.  
  118. class Tower : public Entity
  119. {
  120. public:
  121.     Tower( int x, int y, int id ) : Entity( x, y, id )
  122.             { ptrGrid->Register( id, ExportedClass(), x, y ); }
  123.     Entity * Clone() { return( new Tower( GetX(), GetY(), GetWorldID() ) ); }
  124.  
  125.     int ExportedClass() { return ENTITY_TOWER; }
  126.     bool Update();
  127.     
  128. };
  129.  
  130.  
  131. /***********************************************************
  132.  * CLASS Haven
  133.  * A safe place for the troll to rest (i.e., a dark and
  134.  * foreboding monster-riddern bog).
  135.  **********************************************************/
  136.  
  137. class Haven : public Entity
  138. {
  139. public:
  140.     Haven( int x, int y, int id ) : Entity( x, y, id )
  141.             { ptrGrid->Register( id, ExportedClass(), x, y ); }
  142.     Entity * Clone() { return( new Haven( GetX(), GetY(), GetWorldID() ) ); }
  143.  
  144.     int ExportedClass() { return ENTITY_HAVEN; }
  145.     bool Update() { return true; }
  146. };
  147.  
  148.  
  149. /**********************************************************
  150.  * CLASS Trap
  151.  * A troll-capture device, disguised as a Haven until the 
  152.  * troll detects the trap or falls into it.
  153.  *********************************************************/
  154.  
  155. class Trap : public Entity
  156. {
  157.     // A trap looks just like a safe haven until it is unveiled
  158.     bool Unveiled;
  159.  
  160. public:
  161.     Trap( int x, int y, int id ) : Entity( x, y, id ), Unveiled( false )
  162.             { ptrGrid->Register( id, ExportedClass(), x, y ); }
  163.     Entity * Clone() { return( new Trap( GetX(), GetY(), GetWorldID() ) ); }
  164.  
  165.     int ExportedClass() { return( Unveiled ? ENTITY_TRAP : ENTITY_HAVEN ); }
  166.  
  167.     bool Update();
  168. };
  169.  
  170. #endif